package com.zx.jhpt.login;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zx.framework.plat.dept.po.Dept;
import com.zx.framework.plat.dept.service.DeptService;
import com.zx.framework.plat.user.po.User;
import com.zx.framework.plat.user.service.UserService;
import com.zx.framework.web.controller.BaseController;
import com.zx.framework.web.exception.AjaxException;
import com.zx.jhpt.po.FxDept;
import com.zx.jhpt.po.FxUser;
import com.zx.jhpt.po.Fxfkjz;
import com.zx.jhpt.service.FxDeptService;
import com.zx.jhpt.service.FxfkjzService;
import com.zx.jhpt.util.DeptCache;
@Controller
@RequestMapping("/fxjz/config")
public class FxConfigController extends BaseController {
	
	private static final Logger LOGGER = Logger.getLogger(FxConfigController.class);
	
	@Resource
	private FxfkjzService fxfkjzService;
	@Resource
	private FxDeptService fxDeptService;
	@Resource
	private UserService userService;
	@Resource
	private DeptService deptService;
	/**
	 * 展示矩阵树形
	 * @return
	 */
	@RequestMapping("show")
	public String show(Model model){
		/**
		 * 获取所有矩阵
		 */
		List<Fxfkjz> fxds = fxfkjzService.getAll();
		
		//获取所有部门对应关系
		List<FxDept> fxDepts = fxDeptService.findAll();
		
		Map<String, List<Dept>> deptMap = new HashMap<String, List<Dept>>();
		
		for (FxDept fxDept : fxDepts) {
			String fxid = fxDept.getId().getFxid();
			String deptId = fxDept.getId().getDept();
			List<Dept> depts;
			if(deptMap.containsKey(fxid)){
				depts = deptMap.get(fxid);
			}else{
				depts = new ArrayList<Dept>();
			}
			depts.add(DeptCache.getDept(deptId));
			
			deptMap.put(fxid, depts);
		}
		
		//获取所有部门人员对应关系
		List<FxUser> fxUsers = fxDeptService.findUserAll();
		List<User> users = fxDeptService.findFxUsers();
		
		Map<String, User> usersMap = new HashMap<String, User>();
		for (User user : users) {
			usersMap.put(user.getId(), user);
		}
		
		Map<String, List<User>> fxUserMap = new HashMap<String, List<User>>();
		
		for (FxUser fxUser : fxUsers) {
			String fxid = fxUser.getId().getFxid();
			String deptId = fxUser.getId().getDept();
			
			String key = fxid + deptId;
			
			List<User> tempUsers;
			if(fxUserMap.containsKey(key)){
				tempUsers = fxUserMap.get(key);
			}else{
				tempUsers = new ArrayList<User>();
			}
			
			tempUsers.add(usersMap.get(fxUser.getId().getUserid()));
			fxUserMap.put(key, tempUsers);
		}
		
		model.addAttribute("fxds", fxds);
		model.addAttribute("deptMap", deptMap);
		model.addAttribute("fxUserMap", fxUserMap);
		return "fxjz";
	}
	/**
	 * 删除风险点的负责人
	 * @param userId
	 * @param fxid
	 * @param deptId
	 * @return
	 */
	@RequestMapping("/user/remove")
	@ResponseBody
	public Object removeUser(String userId,String fxid,String deptId){
		try {
			fxDeptService.removeUser(userId, fxid, deptId);
			return clientMap().success().message("删除成功！").getMap();
		} catch (Exception e) {
			LOGGER.error(e);
			throw new AjaxException(e.getMessage());
		}
	}
	/**
	 * 保存风险点负责人
	 * @param userId
	 * @param fxid
	 * @param deptId
	 * @return
	 */
	@RequestMapping("/user/save")
	@ResponseBody
	public Object saveUser(String userId,String fxid,String deptId){
		try {
			User u = userService.findUserById(userId);
			if(u==null){
				throw new AjaxException("设置工号["+userId+"]的用户不存在！");
			}
			fxDeptService.saveUser(userId, fxid, deptId);
			return clientMap().success().message("设置成功！").put("name",u.getUserName()).getMap();
		} catch (Exception e) {
			LOGGER.error(e);
			throw new AjaxException(e.getMessage());
		}
	}
	/**
	 * 删除已设置的部门
	 * @param fxid
	 * @param deptId
	 * @return
	 */
	@RequestMapping("dept/remove")
	@ResponseBody
	public Object removeDept(String fxid,String deptId){
		try {
			fxDeptService.removeDept(fxid, deptId);
			return clientMap().success().message("删除成功！").getMap();
		} catch (Exception e) {
			LOGGER.error(e);
			throw new AjaxException(e.getMessage());
		}
	}
	/**
	 * 保存设置部门
	 * @param fxid
	 * @param deptId
	 * @return
	 */
	@RequestMapping("dept/save")
	@ResponseBody
	public Object saveDept(String fxid,String deptId){
		try {
			Dept d = deptService.findDeptById(deptId);
			if(d==null){
				throw new AjaxException("设置的部门信息不存在！");
			}
			fxDeptService.saveDept(fxid, deptId);
			
			return clientMap().success().message("设置成功！").put("name",d.getName()).getMap();
		} catch (Exception e) {
			LOGGER.error(e);
			throw new AjaxException(e.getMessage());
		}
	}
}
